import config from '../../utils/config'
import {request} from "../../utils/request";
const app = getApp()
Page({
    data: {
        picUrl:config.picUrl,
        custom:wx.getMenuButtonBoundingClientRect(),
        user:{},
        status:'',
        page:1,
        last_page:1,
        list:[],
        endHide:false,
        hideShadow:true,
        hideFahuo:true,
        expressList:[],
        express:{},
        address:'',
        orderIndex:null
    },
    onLoad(options) {
        var user = wx.getStorageSync('userInfo')
        this.setData({user})
        this.getList()
        this.getExpress()
    },
    getList(){
        request({
            url:"/miniprogram/order/getMySaled",
            data:{page:this.data.page,status:this.data.status,user_id:this.data.user.id},
            type:'post',
        }).then(res=>{
            if(this.data.page == 1) this.setData({list:[]})
            this.setData({
                list:this.data.list.concat(res.data),
                last_page:res.last_page
            })
        })
    },
    changeStatus(e){
        var status = e.currentTarget.dataset.status
        this.setData({
            status,
            page:1,
            last_page:1,
            endHide:true
        })
        this.getList()
    },
    onReachBottom() {
        if(this.data.page >= this.data.last_page){
            this.setData({endHide:false})
            return false;
        }
        this.setData({page:this.data.page+1})
        this.getList()
    },
    fahuo(e){
        var {id,index} = e.currentTarget.dataset
        this.setData({
            hideShadow:false,
            hideFahuo:false,
            address:'姓名：' + this.data.list[index].name + ' 地址：' + this.data.list[index].province + this.data.list[index].city + this.data.list[index].county + this.data.list[index].address + ' 电话：' + this.data.list[index].phone,
            orderIndex:index
        })
    },
    getExpress(){
        request({url:"/miniprogram/system/getExpress"}).then(res=>{
            this.setData({expressList:res.data})
        })
    },
    changeExpress(e){
        var express = {
            id:this.data.expressList[e.detail.value].id,
            name:this.data.expressList[e.detail.value].name,
            num:''
        }
        this.setData({express})
    },
    changeValue(e){
        this.setData({
            ['express.num']:e.detail.value
        })
    },
    submitFahuo(){
        wx.showModal({
            title:"确定要发货吗？",
            success:rel=>{
                if(rel.confirm){
                    wx.showLoading()
                    request({
                        url:"/miniprogram/order/fahuo",
                        data:{
                            id:this.data.list[this.data.orderIndex].id,
                            waybill_type:this.data.express.id,
                            waybill:this.data.express.num
                        }
                    }).then(res=>{
                        wx.hideLoading()
                        
                        config.toast(res.msg,res.code,()=>{})
                        if(res.code == 200){
                            var list = this.data.list
                            list[this.data.orderIndex].status = 2
                            this.setData({list,express:{}})
                            this.closeShadow()
                        }

                    })
                }
            }
        })

    },
    copy(e){
        wx.setClipboardData({
            data: e.currentTarget.dataset.txt,
            success(res){}
        })
    },
    scan(){
        wx.scanCode({
            success:(res)=>{
                if(res.errMsg == 'scanCode:ok'){
                    this.setData({['express.num']:res.result})
                    config.toast('我们不保证扫码获取单号的准确性，请扫码后仔细核对！！！',201,()=>{})
                }
            }
        })
    },
    closeShadow(){
        this.setData({
            express:{
                id:'',
                name:'',
                num:''
            },
            address:'',
            orderIndex:null,
            hideShadow:true,
            hideFahuo:true,
        })
    }
})